package com.balloon.logic;

import com.balloon.io.UserDAO;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This bean is used to save the Data needed in the whole application; This data is saved in the form of variables,
 * and each variable has a getter and a setter method 
 * 
 * Variables needed in the application:
 *   First, the Servlet needs to send the 'username' and 'password' of the user to the DAO, to check the user's validity
 *   	So we need two String variables for the username and password (String username, String password)
 *   
 *   Then , the DAO needs to tell the Servlet if the user is valid or not
 *   	So we need a boolean variable that represent that user's validity (boolean isValid)
 *   
 *   Finally, if the user is registered, the DAO needs to tell the JSP about his first and last names
 *   	So we need two String variables for the first and last names (String firstName, String lastName)
 * 
 * */
public class UserBean {

    private String username;
    private String password;
    private String firstName;
    private String lastName;
    private String emailAddress;
    private String phone;
    private boolean valid;
    private ArrayList<UserComments> comments;
    private ArrayList<UserVisitedPlace> visitedplaces;

    /**
     *
     */
    public UserBean() {
        comments = new ArrayList();
        visitedplaces=new ArrayList();
    }
/**
 *
 * @param emailAddress
 */
    public UserBean(String emailAddress) {
        comments = new ArrayList();
        this.emailAddress = emailAddress;
        visitedplaces=new ArrayList();
    }
/**
 * method to get the first name
 * @return firstName
 */
    public String getFirstName() {
        return firstName;
    }

  /**
   * method to set the first name
   * @param newFirstName
   */
    public void setFirstName(String newFirstName) {
        firstName = newFirstName;
    }
/**
 * method to get the last name
 * @return lastName
 */
    public String getLastName() {
        return lastName;
    }

     /**
   * method to set the last name
   * @param newLastName
   */
    public void setLastName(String newLastName) {
        lastName = newLastName;
    }
/**
 * method to get the password
 * @return password
 */
    public String getPassword() {
        return password;
    }

    /**
 * method to set the password
 * @return
 */
    public void setPassword(String newPassword) {
        password = newPassword;
    }

    /**
 * method to get the username
 * @return username
 */
    public String getUsername() {
        return username;
    }

/**
 * method to set the username
 * @param newUsername
 */
    public void setUserName(String newUsername) {
        username = newUsername;
    }
/**
 * method to set  user is valid
 * @return
 */
    public boolean isValid() {
        return valid;
    }
/**
 * method to method to set  user is valid
 * @param newValid
 */
    public void setValid(boolean newValid) {
        valid = newValid;
    }

    /**
     * a method to get the email
     * @return emailAddress
     */
    public String getEmail() {
        return emailAddress;
    }

    /**
     * method to set the email
     * @param newEmail
     */
    public void setEmail(String newEmail) {
        emailAddress = newEmail;
    }
/**
 * method to get the email
 * @return emailAddress
 */
    public String getEmailAddress() {
        return emailAddress;
    }

/**
 * method to get the phone
 * @return phone
 */
    public String getPhone() {
        return phone;
    }

/**
 * method to set the email
 * @param emailAddress
 */
    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    /**
     * method to set the phone
     * @param phone
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    /**
     * method to get the comments
     * @return comments
     */
    public ArrayList<UserComments> getComments() {
        ResultSet rs = UserDAO.getComments(getEmailAddress());
        try {
            while (rs.next()) {
                addComments(rs.getString("cityName"), rs.getString("description"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return comments;

    }

    /**
     * method to add comments
     * @param object
     * @param comments
     */
    public void addComments(String object, String comments) {
        UserComments userComments = new UserComments(object, comments);
        this.comments.add(userComments);
    }

    /**
     * method to get the visited places
     * @return
     * @throws ParseException
     */
    public ArrayList<UserVisitedPlace> getVisitedPlaces() throws ParseException {
        ResultSet rs = UserDAO.getVisitedPlaces(getEmailAddress());
        try {
            while (rs.next()) {
                SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
                java.util.Date d = s.parse(rs.getString("visitedTime"));
                java.sql.Date date = new java.sql.Date(d.getTime());
                System.out.println(date);
                System.out.println(rs.getString("placeCategory"));
                UserVisitedPlace visitedPlace = new UserVisitedPlace(rs.getString("placeCategory"), date);
                this.visitedplaces.add(visitedPlace);
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return visitedplaces;

    }
}

